package com.oritsh.dicom.scu;

import org.dcm4che.data.Attributes;
import org.dcm4che.data.Tag;
import org.dcm4che.data.UID;
import org.dcm4che.net.Dimse;
import org.dcm4che.net.DimseRSP;
import org.dcm4che.net.pdu.PresentationContext;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: zarra
 * Date: 14-2-5
 * Time: 下午1:51
 * Copyright Shanghai Orient Rain Information Technology Co.,Ltd.
 */
public class EchoSCU extends DicomSCU {

    public EchoSCU() {
        super();
    }

    public EchoSCU(String aeCalling) {
        super(aeCalling);
    }

    @Override
    protected PresentationContext createPresentationContext() {
        PresentationContext presentationContext =
                new PresentationContext(1, UID.VerificationSOPClass, IVR_LE_FIRST);
        return presentationContext;
    }

    public boolean echo() {
        try {
            DimseRSP rsp = as.cecho();
            while (rsp.next()) {
                Attributes command = rsp.getCommand();
                if (command != null) {
                    int commandField = command.getInt(Tag.CommandField, -1); //判断是否 CECHO 应答
                    if (commandField == Dimse.C_ECHO_RSP.commandField()) {
                        int status = command.getInt(Tag.Status, -1);
                        if (status == 0)//status 为 0 表示成功
                            return true;
                    }
                }
            }
        } catch (InterruptedException | IOException e) {
            //return false;
        }
        return false;
    }
}
